﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;

public partial class PurviewManage_PurviewsList : System.Web.UI.Page
{
    IRolePurviewHandler rolePurviewManage = new RolePurviewHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1001"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }              
            }
           
            ViewState["OrderBy"] = "PurviewID";
            ViewState["Accending"] = "asc";
            dataBind();
        }
    }


    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string sqlstr,ref SqlParameter[] parms)
    {
       // parms = new SqlParameter[2];
        int i = 0;
        sqlstr = "";
        if (ViewState["PurviewName"] != null)
        {
            parms[0] = new SqlParameter("@PurviewName", ViewState["PurviewName"].ToString());
            sqlstr += " PurviewName like '%' +  @PurviewName + '%'";
        }
        else
        {
            parms[0] = new SqlParameter("@PurviewID" + i.ToString(), "-1");
            sqlstr += " PurviewID != @PurviewID" + i.ToString();
            i += 1;
        }

        if (ViewState["PurviewGroupCode"] != null)
        {
            parms[1] = new SqlParameter("@purviewGroupCode", ViewState["PurviewGroupCode"].ToString());
            sqlstr += " and PurviewGroupCode=@PurviewGroupCode";
        }
        else
        {
            parms[1] = new SqlParameter("@PurviewID" + i.ToString(), "-1");
            sqlstr += " and PurviewID!=@PurviewID" + i.ToString();
        }
    }

    private void dataBind()
    {
        string sqlStr = "SELECT * FROM Purviews where ";
        SqlParameter[] parms = new SqlParameter[2];
        string clause = string.Empty;
        ProcessParms(ref clause,ref parms);
        sqlStr += clause;            

        DataTable dt = rolePurviewManage.GetPurviewList(sqlStr, parms);
        DataView view = dt.DefaultView;
        view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvPurview.DataSource = view;       
        gvPurview.DataBind();
        if (dt != null && dt.Rows.Count > 0)
        {
            lblTotal.Text = dt.Rows.Count.ToString();
        }
        else
        {
            lblTotal.Text = "0";
        }
    }

    //protected void GoPage_TextChanged(object sender, EventArgs e)
    //{
    //    int i = Convert.ToInt32(((DropDownList)sender).Text) - 1;
    //    if (i >= 0 && i < gvPurview.PageCount)
    //    {
    //        this.gvPurview.PageIndex = i;
    //        //数据绑定
    //        dataBind();
    //    }
    //}

    //public int[] CreatSource(int numFrom, int numTo)
    //{
    //    int num = numTo - numFrom + 1;
    //    int[] a = new int[num];
    //    for (int i = 0; i < num; i++) { a[i] = i + numFrom; }
    //    return a;
    //}

    protected void gvPurview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPurview.PageIndex = e.NewPageIndex;
        dataBind();
    }
    protected void gvPurview_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void gvPurview_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtPurviewName.Text.Trim() != string.Empty)
        {
            ViewState["PurviewName"] = txtPurviewName.Text.Trim();
        }
        else
        {
            ViewState["PurviewName"] = null;
        }
        if (txtPurviewGroup.Text.Trim() != string.Empty)
        {
            ViewState["PurviewGroupCode"] = txtPurviewGroup.Text.Trim();
        }
        else
        {
            ViewState["PurviewGroupCode"] = null;
        }

        dataBind();
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvPurview.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvPurview.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvPurview.PageCount)
            {
                this.gvPurview.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax("请输入大于0小于总页数的正整数!", UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax("请输入大于0小于总页数的正整数!",UpdatePanel1);
        }
    }
}
